// Class1.cs
//

using System;
using System.DHTML;
using Sys;
using System.Silverlight;

namespace SBT
{
    /// <summary>
    /// Handles loading and global Silverlight plugin event management
    /// Can be overriden
    /// </summary>
    public class SLManager : Sys.UI.Behavior
    {

        private ControlLoadCallback m_loadCallback;
        private ControlErrorCallback m_errorCallback;
        private SilverlightControl m_plugin;
        public SilverlightControl Plugin
        {
            get { return m_plugin; }
        }
        /// <summary>
        /// Build the SLManager
        /// </summary>
        /// <param name="host">HTML element hosting the Silverlight plugin</param>
        public SLManager(DOMElement host)
            : base(host)
        { }

        private double m_width;
        private double m_height;
        private string m_xamlPath;
        /// <summary>
        /// Width of the Silverlight control (in px)
        /// </summary>
        public double Width { get { return m_width; } set { m_width = value; } }
        /// <summary>
        /// Height of the Silverlight control (in px)
        /// </summary>
        public double Height { get { return m_height; } set { m_height = value; } }
        /// <summary>
        /// Path of the xaml source
        /// </summary>
        public string XamlPath { get { return m_xamlPath; } set { m_xamlPath = value; } }

        /// <summary>
        /// Called when the silverlight plugin is loaded
        /// </summary>
        /// <param name="slControl"></param>
        /// <param name="args"></param>
        public virtual void OnSilverlightLoaded(SilverlightControl slControl, object args)
        {
        }

        /// <summary>
        /// Called on Silverlight error
        /// </summary>
        /// <param name="slControl"></param>
        /// <param name="args"></param>
        public virtual void OnSilverlightError(SilverlightControl slControl, ControlError args)
        {
        }

        public override void Initialize()
        {
            base.Initialize();
            m_loadCallback = new ControlLoadCallback(OnSilverlightLoaded);
            m_errorCallback= new ControlErrorCallback(OnSilverlightError);
            ControlParameters createParams = new ControlParameters(XamlPath, this.Element);
            createParams.windowless = true;
            createParams.style = "width:" + Width + "px; height:" + Height + "px;";
            m_plugin = ControlFactory.CreateSilverlight(createParams, m_loadCallback, m_errorCallback);
        }
        
    }
}
